package com.gopay.common.domain.rcs.transadmit;

import java.util.Arrays;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.gopay.common.domain.BaseEntity;
import com.gopay.common.util.compare.Compare;
import org.apache.commons.lang3.StringUtils;

/**
 * 交易准入配置PO
 * @author lizheng
 *
 */
@Entity
@Table(name = "RCS_TRANS_ADMITTANCE_CONF_CUST")
public class RcsTransAdmittanceConfCust extends BaseEntity {

	private static final long serialVersionUID = 1L;

	@Id
	@SequenceGenerator(name = "RcsTransAdmittanceConfSequence", sequenceName = "RCS_TRANS_ADMITTANCE_CONF_SEQ")
	@GeneratedValue(generator = "RcsTransAdmittanceConfSequence", strategy = GenerationType.SEQUENCE)
	@Column(name = "SQ", nullable = false, unique = true)
	private int confSq;
	@Column(name = "CUST_ID", nullable = false)
	private String custId;
	@Column(name = "GOPAY_INT_TXN_CD", nullable = false)
	private String gopayIntTxnCd;
	@Column(name = "WEB_SINGLE_PERM")
	@Compare(columnName = "portal 单笔")
	private String webSinglePerm;
	@Column(name = "WEB_BATCH_PERM")
	@Compare(columnName = "portal 批量")
	private String webBatchPerm;
	@Column(name = "API_SINGLE_PERM")
	@Compare(columnName = "gateway api")
	private String apiSinglePerm;
	@Column(name = "IP_SWITCH")
	private String ipSwitch;
	@Column(name = "ADMIT_IP")
	@Compare(columnName = "交易IP备案")
	private String admitIp;
	@Column(name = "CREATED_TS", nullable = true, unique = false)
	private Date createdTs = new Date();
	@Column(name = "LAST_UPD_TS", nullable = true, unique = false)
	private Date lastUpdTs = new Date();
	@Column(name = "LAST_UPD_TRANCODE", nullable = true, unique = false)
	private String lastUpdTrancode;

	public String getCustId() {
		return custId;
	}

	public void setCustId(String custId) {
		this.custId = custId;
	}

	public String getIpSwitch() {
		return ipSwitch;
	}

	public void setIpSwitch(String ipSwich) {
		this.ipSwitch = ipSwich;
	}

	public String getAdmitIp() {
		return admitIp;
	}

	public void setAdmitIp(String admitIp) {
		this.admitIp = admitIp;
	}

	public int getConfSq() {
		return confSq;
	}

	public void setConfSq(int confSq) {
		this.confSq = confSq;
	}

	public String getGopayIntTxnCd() {
		return gopayIntTxnCd;
	}

	public void setGopayIntTxnCd(String gopayIntTxnCd) {
		this.gopayIntTxnCd = gopayIntTxnCd;
	}

	public String getWebSinglePerm() {
		return webSinglePerm;
	}

	public void setWebSinglePerm(String webSinglePerm) {
		this.webSinglePerm = webSinglePerm;
	}

	public String getWebBatchPerm() {
		return webBatchPerm;
	}

	public void setWebBatchPerm(String webBatchPerm) {
		this.webBatchPerm = webBatchPerm;
	}

	public String getApiSinglePerm() {
		return apiSinglePerm;
	}

	public void setApiSinglePerm(String apiSinglePerm) {
		this.apiSinglePerm = apiSinglePerm;
	}

	public Date getCreatedTs() {
		return createdTs;
	}

	public void setCreatedTs(Date createdTs) {
		this.createdTs = createdTs;
	}

	public Date getLastUpdTs() {
		return lastUpdTs;
	}

	public void setLastUpdTs(Date lastUpdTs) {
		this.lastUpdTs = lastUpdTs;
	}

	public String getLastUpdTrancode() {
		return lastUpdTrancode;
	}

	public void setLastUpdTrancode(String lastUpdTrancode) {
		this.lastUpdTrancode = lastUpdTrancode;
	}

	public static String webSinglePermTranslate(String webSinglePerm){
		if("0".equals(webSinglePerm)){
			return "关闭";
		} else if("1".equals(webSinglePerm)){
			return "开通";
		}
		return "";
	}

	public static String webBatchPermTranslate(String webBatchPerm){
		if("0".equals(webBatchPerm)){
			return "关闭";
		} else if("1".equals(webBatchPerm)){
			return "开通";
		}
		return "";
	}

	public static String apiSinglePermTranslate(String apiSinglePerm){
		if("0".equals(apiSinglePerm)){
			return "关闭";
		} else if("1".equals(apiSinglePerm)){
			return "开通";
		}
		return "";
	}

	public static boolean admitIpEquals(String admitIpBefore,String admitIpAfter){
		if(StringUtils.isNotBlank(admitIpBefore) && StringUtils.isNotBlank(admitIpAfter)) {
			String[] admitIpBeforeArray = admitIpBefore.split(";");
			Arrays.sort(admitIpBeforeArray);
			String[] admitIpAfterArray = admitIpAfter.split(";");
			Arrays.sort(admitIpAfterArray);
			if(admitIpBeforeArray.length == admitIpAfterArray.length){
				for(int i = 0; i < admitIpBeforeArray.length;i++){
					if(! StringUtils.equals(admitIpBeforeArray[i],admitIpAfterArray[i])){
						return false;
					}
				}
				return true;
			}
		}else if(StringUtils.isBlank(admitIpBefore)) {
			if(StringUtils.isBlank(admitIpAfter)) {
				return true;
			}
		}
		return false;
	}

	public static String admitIpTranslate(String admitIp){
		if(null != admitIp && ! "".equals(admitIp)){
			return admitIp;
		}
		return "无";
	}

}
